package com.github.binarywang.demo.wx.mp.handler;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.github.binarywang.demo.wx.mp.builder.TextBuilder;
import com.github.binarywang.demo.wx.mp.service.WeixinService;

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.mayt.wxfl.service.UrltransferService;

/**
 * @author Binary Wang
 */
@Component
public class MsgHandler extends AbstractHandler {
	
//	@Autowired
	UrltransferService urltransferBusiService;

  @Override
  public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                  Map<String, Object> context, WxMpService wxMpService,
                                  WxSessionManager sessionManager) {

	  WeixinService weixinService = (WeixinService) wxMpService;
		// 回复内容
		String content = "";
		if (!wxMessage.getMsgType().equals(WxConsts.XmlMsgType.EVENT)) {
		}
		
		// 当用户输入关键词如“你好”，“客服”等，并且有客服在线时，把消息转发给在线客服
		 if (StringUtils.startsWithAny(wxMessage.getContent(), "你好", "客服")
		 && weixinService.hasKefuOnline()) {
		 return WxMpXmlOutMessage
		 .TRANSFER_CUSTOMER_SERVICE().fromUser(wxMessage.getToUser())
		 .toUser(wxMessage.getFromUser()).build();
		 } else if (wxMessage.getContent().indexOf("jd.com") >= 0){
			 
		 }

		if (StringUtils.isBlank(content)) {
			return null;
		}
		return new TextBuilder().build(content, wxMessage, weixinService);

  }

}
